package com.axend.aerosense.dev.viewmodel;

import android.app.Activity;
import android.app.Dialog;

import androidx.lifecycle.MutableLiveData;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.launcher.ARouter;
import com.axend.aerosense.base.bean.BaseDevInfoBean;
import com.axend.aerosense.base.bean.BaseStringBean;
import com.axend.aerosense.base.impl.IBaseView;
import com.axend.aerosense.base.impl.ILoadDataResult;
import com.axend.aerosense.base.impl.IOperationResult;
import com.axend.aerosense.base.livedatabus.LiveDatabus;
import com.axend.aerosense.base.viewmodel.CustomBaseViewModel;
import com.axend.aerosense.common.UrlApi;
import com.axend.aerosense.common.bean.MoveBindRoomBean;
import com.axend.aerosense.common.config.Gobal;
import com.axend.aerosense.common.router.RouterActivityPath;
import com.axend.aerosense.common.services.ILoginService;
import com.axend.aerosense.common.services.config.ServicesConfig;
import com.axend.aerosense.common.ui.LoadingDialog;
import com.axend.aerosense.common.util.JsonUtil;
import com.axend.aerosense.network.EasyHttp;
import com.axend.aerosense.network.cache.model.CacheMode;
import com.axend.aerosense.network.callback.ProgressDialogCallBack;
import com.axend.aerosense.network.callback.SimpleCallBack;
import com.axend.aerosense.network.exception.ApiException;
import com.axend.aerosense.network.subsciber.IProgressDialog;
import com.axend.aerosense.dev.entity.BindRoomListBean;
import com.axend.aerosense.dev.entity.DeviceBindRoomBean;
import com.axend.aerosense.dev.R;
import com.blankj.utilcode.util.ToastUtils;

import io.reactivex.disposables.Disposable;

/**
 * @author ChenHongWei
 * @date :2021/1/22 16:32
 */

public class BindRoomViewModel extends CustomBaseViewModel<BindRoomListBean> {

    private MutableLiveData<Boolean> isComplete = new MutableLiveData<>(false);  //是否完成添加设备

    @Autowired(name = ServicesConfig.User.LONGING_SERVICE)
    ILoginService iLoginService;

    private Disposable disposable;

    public BindRoomViewModel(IBaseView baseViewImpl, ILoadDataResult<BindRoomListBean> loadDataResultImpl) {
        super(baseViewImpl, loadDataResultImpl);
        ARouter.getInstance().inject(this);
    }

    public void bindRoom(DeviceBindRoomBean bean, Activity activity){
        disposable = EasyHttp.post(UrlApi.URL.SetRadarBindRoom.getUrl())
                .headers("token", iLoginService.getToken())
                .cacheMode(CacheMode.NO_CACHE)
                .upJson(JsonUtil.toJson(bean))
                .execute(new ProgressDialogCallBack<BaseStringBean>(LoadingDialog.getProgressDialog(activity)) {
                    @Override
                    public void onTokenExpire() {
                        dismissProgress();
                        ToastUtils.showShort(R.string.common_login_first);
                        //登录界面不做处理
                        ARouter.getInstance().build(RouterActivityPath.Login.PAGE_LOGIN_HOME).withBoolean("jumpMainPage", true).greenChannel().navigation();
                    }

                    @Override
                    public void onError(ApiException e) {
                        dismissProgress();
                        e.printStackTrace();
                        loadDataFail(e.getMessage());
                        if (e.getCode() == 101){
                            ToastUtils.showShort(R.string.dev_device_binding_room);
                        }else{
                            ToastUtils.showShort(e.getMessage());
                        }
                    }

                    @Override
                    public void onSuccess(BaseStringBean bean) {
                        dismissProgress();
                        isComplete.setValue(true);
                        LiveDatabus.getInstance().with(Gobal.EVENT_KEY.REFESH_ROOM, Integer.class).postValue(1);
                        LiveDatabus.getInstance().with(Gobal.EVENT_KEY.DEV_COUNT_CHANGE, Integer.class).postValue(1);
                    }
                });
    }

    // 在设备设置界面重新绑定房间
    public void reBindRoom(Activity activity, MoveBindRoomBean bean){
        disposable = EasyHttp.post(UrlApi.URL.MoveRadars.getUrl())
                .headers("token", iLoginService.getToken())
                .cacheMode(CacheMode.NO_CACHE)
                .upJson(JsonUtil.toJson(bean))
                .execute(new ProgressDialogCallBack<BaseStringBean>(LoadingDialog.getProgressDialog(activity)) {
                    @Override
                    public void onTokenExpire() {
                        dismissProgress();
                        ToastUtils.showShort(R.string.common_login_first);
                        //登录界面不做处理
                        ARouter.getInstance().build(RouterActivityPath.Login.PAGE_LOGIN_HOME).withBoolean("jumpMainPage", true).greenChannel().navigation();
                    }

                    @Override
                    public void onError(ApiException e) {
                        dismissProgress();
                        e.printStackTrace();
                        ToastUtils.showShort(e.getMessage());
                    }

                    @Override
                    public void onSuccess(BaseStringBean bean) {
                        dismissProgress();
                        ToastUtils.showShort(R.string.common_success);
                        LiveDatabus.getInstance().with(Gobal.EVENT_KEY.REFESH_ROOM, Integer.class).postValue(1);
                        LiveDatabus.getInstance().with(Gobal.EVENT_KEY.DEV_COUNT_CHANGE, Integer.class).postValue(1);
                        isComplete.setValue(true);
                    }
                });
    }

    @Override
    public void onLoad() {
        disposable = EasyHttp.post(UrlApi.URL.GetBindRadarRoom.getUrl())
                .headers("token", iLoginService.getToken())
                .cacheMode(CacheMode.NO_CACHE)
                .upJson(JsonUtil.toJson(new BaseDevInfoBean()))
                .execute(new SimpleCallBack<BindRoomListBean>() {
                    @Override
                    public void onTokenExpire() {

                    }

                    @Override
                    public void onError(ApiException e) {
                        ToastUtils.showShort(e.getMessage());
                        loadDataFail(e.getMessage());
                    }

                    @Override
                    public void onSuccess(BindRoomListBean bindRoomBean) {
                        loadDataSuccess(bindRoomBean);
                        // 下次加载数据认为是刷新
                        isRefresh = true;
                    }
                });
    }

    public MutableLiveData<Boolean> getIsComplete() {
        return isComplete;
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        if (null != disposable && !disposable.isDisposed()){
            disposable.dispose();
        }
    }

}
